package com.wwj.core.server.service.impl;

import com.wwj.core.server.entity.ControlServer;
import com.wwj.core.server.entity.mapper.ControlServerMapper;
import com.wwj.core.server.service.IControlServerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ControlServerServiceImpl implements IControlServerService {

    @Resource
    private ControlServerMapper controlServerMapper;

    @Override
    public ControlServer findByServerName(String serverName) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("serverName", serverName);
        List<ControlServer> list = controlServerMapper.selectByParam(param);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public ControlServer findByIp(String ip) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("ip", ip);
        List<ControlServer> list = controlServerMapper.selectByParam(param);
        return null != list && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public int createNewServer(ControlServer controlServer) throws Exception {
        controlServer.setCreateTime(Calendar.getInstance().getTime());
        return controlServerMapper.save(controlServer);
    }

    @Override
    public int updateServer(ControlServer controlServer) throws Exception {
        controlServer.setUpdateTime(Calendar.getInstance().getTime());
        return controlServerMapper.update(controlServer);
    }
}
